package com.songmy.spider.run.vo;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/***
 * 字段,包含采集的字段名
 *
 * @author Marc
 * @version 1.0
 */
public class Field {

	private String fieldName = null ; 
	
	private String fieldCode = null ; 
	
	/***
	 * 选择器类型,默认为正则选择器
	 */
	private MySelectorType  selectorType = MySelectorType.XPATH;
	
	/***
	 * 选择规则
	 */
	private String selectorRule = null ; 
	
	/***
	 * 字段是否必有,若为True 则 丢弃掉整条数据
	 */
	private boolean required =  false;
	
	/***
	 * 字段是否为多项,如字段为多项,则返回List数组
	 */
	private boolean hasMore = false;
	
	
	/***
	 * 子字段
	 */
	private List<Field> childFied = null ; 
	
	/***
	 * 内容替换
	 */
	private Map<String,Object> replace = null ; 
	
	
	/***
	 * 异步URL
	 */
	private String jsUrl  = null ; 
	
	/***
	 * 异步请求方式
	 */
	private JSMethod jsMethod = JSMethod.GET;
	
	/***
	 * 异步参数
	 */
	private String jsParams = null ; 
	
	/***
	 * 异步请求头
	 */
	private Map<String,String> head =  null ;

	/**
	 * @return the replace
	 */
	public Map<String, Object> getReplace() {
		return replace;
	}

	/**
	 * @param replace the replace to set
	 */
	public void setReplace(Map<String, Object> replace) {
		this.replace = replace;
	}

	/**
	 * @return the fieldName
	 */
	public String getFieldName() {
		return fieldName;
	}

	/**
	 * @return the fieldCode
	 */
	public String getFieldCode() {
		return fieldCode;
	}

	/**
	 * @return the selectorType
	 */
	public MySelectorType getSelectorType() {
		return selectorType;
	}

	/**
	 * @return the selectorRule
	 */
	public String getSelectorRule() {
		return selectorRule;
	}

	/**
	 * @return the required
	 */
	public boolean isRequired() {
		return required;
	}

	/**
	 * @return the hasMore
	 */
	public boolean isHasMore() {
		return hasMore;
	}

	/**
	 * @return the childFied
	 */
	public List<Field> getChildFied() {
		return childFied;
	}

	/**
	 * @return the jsUrl
	 */
	public String getJsUrl() {
		return jsUrl;
	}

	/**
	 * @return the jsMethod
	 */
	public JSMethod getJsMethod() {
		return jsMethod;
	}

	/**
	 * @return the jsParams
	 */
	public String getJsParams() {
		return jsParams;
	}

	/**
	 * @return the head
	 */
	public String getHeadStr() {
		if(this.head.size() > 0 ){
			StringBuffer headStr = new StringBuffer();
			Iterator<String> ite = head.keySet().iterator();
			while (ite.hasNext()) {
				String key = ite.next();
				String value = head.get(key);
				headStr.append(key+":"+value+"\r\n");
			}
			return headStr.toString();
		}else{
			return "";
		}
	}

	/**
	 * @param fieldName the fieldName to set
	 */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	/**
	 * @param fieldCode the fieldCode to set
	 */
	public void setFieldCode(String fieldCode) {
		this.fieldCode = fieldCode;
	}

	/**
	 * @param selectorType the selectorType to set
	 */
	public void setSelectorType(MySelectorType selectorType) {
		this.selectorType = selectorType;
	}

	/**
	 * @param selectorRule the selectorRule to set
	 */
	public void setSelectorRule(String selectorRule) {
		this.selectorRule = selectorRule;
	}

	/**
	 * @param required the required to set
	 */
	public void setRequired(boolean required) {
		this.required = required;
	}

	/**
	 * @param hasMore the hasMore to set
	 */
	public void setHasMore(boolean hasMore) {
		this.hasMore = hasMore;
	}

	/**
	 * @param childFied the childFied to set
	 */
	public void setChildFied(List<Field> childFied) {
		this.childFied = childFied;
	}

	/**
	 * @param jsUrl the jsUrl to set
	 */
	public void setJsUrl(String jsUrl) {
		this.jsUrl = jsUrl;
	}

	/**
	 * @param jsMethod the jsMethod to set
	 */
	public void setJsMethod(String method) {
		this.jsMethod = JSMethod.valueOf(method);
	}

	/**
	 * @param jsParams the jsParams to set
	 */
	public void setJsParams(String jsParams) {
		this.jsParams = jsParams;
	}

	/**
	 * @param head the head to set
	 */
	public void addHead(String key,String value) {
		this.head.put(key, value);
	}
	
	/**
	 * @param head the head to set
	 */
	public void setHead(Map<String, String> head) {
		this.head = head;
	}
	
	
}
